﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class Mod_StockManage_frm_StockIn : System.Web.UI.Page
{
    private static string strType = "4";//代表采购入库

    private void ControlButtonVisible(string UserLogID, string CurrPageID)
    {
        //处理系统参数设置
        ClsSystemSetDAL cls_SystemSet = new ClsSystemSetDAL();
        this.hidSystemParam.Value = cls_SystemSet.GetSystemParamSetText(2);

        ClsMenuDAL cls_Menu = new ClsMenuDAL();
        DataSet dst = cls_Menu.GetCurrUserButtonPopedom(UserLogID, CurrPageID);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            int intCount = dst.Tables[0].Rows.Count - 1;

            for (int i = intCount; i >= 0; i--)
            {
                int intButtonID = Int32.Parse(dst.Tables[0].Rows[i][2].ToString());

                switch (intButtonID)
                {
                    case 0:
                        this.panAdd.Visible = true;
                        break;
                    case 1:
                        this.hidButtonEdit.Value = intButtonID.ToString();
                        break;
                    case 2:
                        this.panDelete.Visible = true;
                        break;
                    case 3:
                        this.lbtnDelete1.Enabled = true;
                        break;
                    case 4:
                        this.panAudit.Visible = true;
                        break;
                    case 5:
                        this.hidButtonOAudit.Value = intButtonID.ToString();
                        break;
                    case 6:
                        this.panRef.Visible = true;
                        break;
                    case 7:
                        this.panSearch.Visible = true;
                        break;
                    default:
                        //this.panEnd.Visible = true;
                        break;
                }
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        string strUserID = this.getCurUserId();

        if (strUserID != "")
        {
            if (!IsPostBack)
            {
                ////////////////按钮权限控制 Begin /////////////////////////
                string strCurrPageID = this.Page.Request.Params["PageID"];
                this.ControlButtonVisible(strUserID, strCurrPageID);
                //////////////////////  End ///////////////////////////////

                this.GetStockInInfoByMain(strUserID);
            }
            else
            {
                this.lblJs.Text = "";//清空Js脚本
                this.labMessage.Text = "";
            }

            this.ibtnDelete0.Attributes.Add("onClick", "return confirm('您真的要删除所选的项吗？');");
            this.lbtnDelete0.Attributes.Add("onClick", "return confirm('您真的要删除所选的项吗？');");
            this.lbtnDelete1.Attributes.Add("onClick", "return confirm('彻底删除将无法恢复数据。您真的要删除所选的项吗？');");
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.Page.RegisterClientScriptBlock("Js", "<script>parent.window.location.href='../index.aspx'</script>");
        }
    }

    public void ChangPage(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = this.dgrdResult.CurrentPageIndex;
            int intPageCounts = this.dgrdResult.PageCount;

            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindStockInByDataView(dst, intPageIndex);
        }
    }

    public void ChildChangPage(object sender, EventArgs e)
    {
        //获取保存的从表中的结果集和DataGrid控件
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetChildResultDataSet;
        DataGrid dgrd = cls_Property.SetOrGetChildDataGridControl;

        if (dst != null && dgrd != null)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = dgrd.CurrentPageIndex;
            int intPageCounts = dgrd.PageCount;

            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindStockInChildByDataView(dgrd, dst, intPageIndex);
        }
    }

    public void AddStockInInfo(object sender, EventArgs e)
    {
        try
        {
            ClsStockManageDAL cls_StockManage = new ClsStockManageDAL();
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            ClsMenuDAL cls_Menu = new ClsMenuDAL();
            string strUserID = cls_Property.SetOrGetUserID;

            if (strUserID != "")
            {
                ImageButton ibtnAdd = (ImageButton)sender;
                int intCommandName = Int32.Parse(ibtnAdd.CommandName);

                if (intCommandName == 0)//打开主表添加面板
                {
                    //获取系统编码
                    string strCode = cls_Menu.GetSystemCode("0", "00", "", 11);
                    if (strCode != "Error")
                    {
                        this.panAddResult.Visible = true;
                        this.ctbAdd0.Text = strCode;
                        this.ctsbAdd0.Text = System.DateTime.Today.ToShortDateString();
                        this.ctsbAdd1.Text = System.DateTime.Today.ToShortDateString();
                        this.lblJs.Text = "<script>document.getElementById('" + this.ctsbAdd0.ClientID + "_txtBox').focus();</script>";
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('新增记录失败，请重试！')</script>";
                    }                    
                }
                else if (intCommandName == 1)//关闭主表添加面板
                {
                    this.panAddResult.Visible = false;
                }
                else if (intCommandName == 2)//保存主表添加记录
                {
                    string Dm_No = this.ctbAdd0.Text.Trim();
                    string Dm_Date = this.ctsbAdd0.Text.Trim();
                    string Dm_Mdate = this.ctsbAdd1.Text.Trim();
                    string Ct_Id = this.ctsbAdd2.Text.Trim();//供应商
                    string Pm_Id = this.ctsbAdd3.Text.Trim();//
                    string Sm_Id = this.ctsbAdd4.Text.Trim();
                    string Dp_Id = this.ctsbAdd6.Text.Trim();//采购部门
                    string Wk_Id = this.ctsbAdd7.Text.Trim();//采购员
                    string wh_id = this.ctsbAddDepot.Text.Trim();//仓库
                    string Dm_Name = this.ctbAdd1.Text.Trim();
                    string Dm_Tel = this.ctbAdd2.Text.Trim();
                    string Dm_Addr = this.ctbAdd3.Text.Trim();
                    string Remark = this.ctbAdd4.Text.Trim();

                    if (Ct_Id.Length == 0)
                    {
                        this.labMessage.Text = "<script>alert('供应商不能为空！');</script>";
                    }
                    else if (wh_id.Length == 0)
                    {
                        this.labMessage.Text = "<script>alert('仓库不能为空！');</script>";
                    }
                    else
                    {
                        Boolean bolIsSucceed = cls_StockManage.AddOrUpdateStockOrder(strType, Dm_No, DateTime.Parse(Dm_Date),
                            DateTime.Parse(Dm_Mdate), Ct_Id, Pm_Id, Sm_Id, wh_id, Dp_Id, Wk_Id, Dm_Name, Dm_Tel, Dm_Addr,
                            strUserID, Remark, 0);

                        if (bolIsSucceed)
                        {
                            this.GetStockInInfoByMain(strUserID);//重新绑定结果

                            this.ctsbAdd0.Text = ctsbAdd1.Text = ctsbAdd2.Text = ctsbAdd3.Text = ctsbAdd4.Text = ctsbAdd6.Text = ctsbAdd7.Text = this.ctbAdd1.Text = this.ctbAdd2.Text = this.ctbAdd3.Text = this.ctbAdd4.Text = "";

                            //主表新增后直接打开从表进行新增从表记录 Begin///////////////////////////
                            this.panAddResult.Visible = false;
                            ImageButton ibtn = new ImageButton();

                            int intItems = this.dgrdResult.Items.Count - 1;

                            for (int i = intItems; i >= 0; i--)
                            {
                                string strRs_DmNO = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRs0")).Text.Trim();

                                if (strRs_DmNO == Dm_No)
                                {
                                    ibtn = (ImageButton)this.dgrdResult.Items[i].FindControl("ibtnOpenChild");
                                    break;
                                }
                            }

                            this.OpenChildResult(ibtn, null);
                            this.AddStockInInfo(this.ibtnAdd, null);
                            //主表新增后直接打开从表进行新增从表记录 End///////////////////////////
                        }
                        else
                        {
                            this.labMessage.Text = "<script language='javascript'>alert('新增记录失败，请重试！');</script>！";
                        }
                    }
                }
                //////////////////////////////////////////从表相关操作/////////////////////////////////////
                else
                {
                    int intIndex = Int32.Parse(this.ibtnAdd.CommandArgument);
                    Panel panChildAddResult = (Panel)this.dgrdResult.Items[intIndex].FindControl("panChildAddResult");                    

                    if (intCommandName == 3)//关闭从表添加面板
                    {
                        panChildAddResult.Visible = false;
                    }
                    else if (intCommandName == 4)//保存从表添加记录
                    {
                        Boolean bolIsSucceed = false;
                        CuTextBox Dm_No = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs0");
                        CuTextSelectBox Im_No = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbChildAdd0");//商品编号
                        CuTextBox Qty = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildAdd4");                        
                        CuTextBox ctbChildAddBatchNo = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildAddBatchNo");//批号                                                
                        CuTextBox ctbPrice = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildAdd3");//单价  
                        CuTextBox ctbSellPrice = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildAddSellPrice");//售价  
                        CuTextBox Remark = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildAdd8");                                                                      
                                                
                        string strIm_no = Im_No.Text.Trim();
                        string strQty = ((CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildAdd4")).Text.Trim().ToString();//数量
                        if (strIm_no.Length == 0 || strQty.Length == 0)
                        {
                            this.labMessage.Text = "<script language='javascript'>alert('商品或者数量不能为空！');</script>！";   
                        }
                        else if (decimal.Parse(strQty) == 0)
                        {
                            this.labMessage.Text = "<script language='javascript'>alert('数量应大于“0”！');</script>！";   
                        }
                        else
                        {                            
                            Boolean bStart = Boolean.Parse(((CuTextBox)this.dgrdResult.Items[intIndex].FindControl("is_Start")).Text.Trim());//是否启用批号
                            string str_BatchNo = ctbChildAddBatchNo.Text.Trim();
                            if (bStart)//如果启用批号，则必须需入批号。否则不允许保存
                            {
                                if (str_BatchNo.Length == 0)
                                {
                                    this.labMessage.Text = "<script language='javascript'>alert('此商品批号不能为空！');</script>！";                                    
                                }
                                else
                                {
                                    bolIsSucceed = cls_StockManage.AddOrUpdateStockOrderChild(-1,strType, Dm_No.Text,Im_No.Text, decimal.Parse(Qty.Text),
                                                decimal.Parse(ctbPrice.Text.Trim()), decimal.Parse(ctbSellPrice.Text.Trim()), ctbChildAddBatchNo.Text.Trim(), Remark.Text, 0);

                                    ctbChildAddBatchNo.Text = "";
                                    if (bolIsSucceed)
                                    {                                        
                                        Im_No.Text = Qty.Text = Remark.Text = "";

                                        DataSet dst = cls_StockManage.GetStockOrderChilds(Dm_No.Text, strType);//重新绑定从表结果

                                        if (dst != null && dst.Tables[0].Rows.Count > 0)
                                        {
                                            DataGrid dgrd = (DataGrid)this.dgrdResult.Items[intIndex].FindControl("dgrdChildResult");

                                            this.BindStockInChildByDataView(dgrd, dst, 0);
                                        }

                                        ((ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnOpenChild")).Attributes.Remove("OnClick");

                                        this.lblJs.Text = "<script>document.getElementById('" + Im_No.ClientID + "_txtBox').focus();</script>";
                                    }
                                    else
                                    {
                                        this.labMessage.Text = "<script language='javascript'>alert('此商品信息已存在！');</script>！";
                                    }
                                }

                            }
                            else
                            {
                                bolIsSucceed = cls_StockManage.AddOrUpdateStockOrderChild(-1,strType, Dm_No.Text,Im_No.Text, decimal.Parse(Qty.Text),
                                    decimal.Parse(ctbPrice.Text.Trim()), decimal.Parse(ctbSellPrice.Text.Trim()), ctbChildAddBatchNo.Text.Trim(), Remark.Text, 0);

                                ctbChildAddBatchNo.Text = "";

                                if (bolIsSucceed)
                                {                                    
                                    Im_No.Text = Qty.Text = Remark.Text = "";

                                    DataSet dst = cls_StockManage.GetStockOrderChilds(Dm_No.Text, strType);//重新绑定从表结果

                                    if (dst != null && dst.Tables[0].Rows.Count > 0)
                                    {
                                        DataGrid dgrd = (DataGrid)this.dgrdResult.Items[intIndex].FindControl("dgrdChildResult");

                                        this.BindStockInChildByDataView(dgrd, dst, 0);
                                    }

                                    ((ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnOpenChild")).Attributes.Remove("OnClick");

                                    this.lblJs.Text = "<script>document.getElementById('" + Im_No.ClientID + "_txtBox').focus();</script>";
                                }
                                else
                                {
                                    this.labMessage.Text = "<script language='javascript'>alert('此商品信息已存在！');</script>！";                                    
                                }
                            }
                        }
                    }
                    else if (intCommandName == 5)//打开从表添加面板
                    {
                        panChildAddResult.Visible = true;                                               
                        CuTextSelectBox ctsbChildAdd0 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbChildAdd0");
                        this.lblJs.Text = "<script>document.getElementById('" + ctsbChildAdd0.ClientID + "_txtBox').focus();</script>";
                    }
                }
            }
            else
            {
                this.Page.Session.Remove("SetOrGetUserID");
                this.Page.RegisterClientScriptBlock("Js", "<script>parent.window.location.href='../index.aspx'</script>");
            }
        }
        catch
        {
            this.labMessage.Text = "<script language='javascript'>alert('请输入必填项或正确的格式！');</script>！";
        }
    }

    public void UpDateStockInInfo(object sender, EventArgs e)
    {
        try
        {
            ClsStockManageDAL cls_StockManage = new ClsStockManageDAL();
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            string strUserID = cls_Property.SetOrGetUserID;

            if (strUserID != "")
            {
                ImageButton ibtnUpdate = (ImageButton)sender;
                int intCommandName = Int32.Parse(ibtnUpdate.CommandName);
                int intIndex = Int32.Parse(ibtnUpdate.CommandArgument);

                if (intCommandName == 0)//主表确认
                {
                    int intCount = this.dgrdResult.Items.Count;

                    for (int i = 0; i < intCount; i++)
                    {
                        bool IsChecked = ((CheckBox)this.dgrdResult.Items[i].FindControl("chkRsSelect")).Checked;

                        if (IsChecked)
                        {
                            string strDm_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRs0")).Text.Trim();                            
                            Boolean bolIsSucceed = cls_StockManage.UpdateStockOrderState(strDm_No, 0, strUserID, strType);

                            if (!bolIsSucceed)
                            {
                                this.labMessage.Text = "<script language='javascript'>alert('确认失败，请重试！');</script>！";                                
                            }
                        }
                    }
                    this.GetStockInInfoByMain(strUserID);//重新绑定结果
                }
                else if (intCommandName == 3)//主表取消确认
                {
                    string strDm_No = ((CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs0")).Text.Trim();
                    //判断是否可以取消确认                    
                    Boolean bReturnAuditState = cls_StockManage.pubWhetherNonAudit(strType, strDm_No, "tboDueM", "tboStockAffirmM", "dm_no", "dm_type");
                    if (!bReturnAuditState)
                    {
                        Boolean bolIsSucceed = cls_StockManage.UpdateStockOrderState(strDm_No, 1, strUserID, strType);
                        if (bolIsSucceed)
                        {
                            this.GetStockInInfoByMain(strUserID);//重新绑定结果
                        }
                        else
                        {
                            this.labMessage.Text = "<script language='javascript'>alert('取消确认失败，请重试！');</script>！";                            
                        }
                    }
                    else
                    {
                        this.labMessage.Text = "<script language='javascript'>alert('入库确认已确认，不能取消确认！');</script>";
                    }                                       
                }
                else if (intCommandName == 1 || intCommandName == 2 || intCommandName == 4)//主表编辑
                {
                    CheckBox chkRsSelect = (CheckBox)this.dgrdResult.Items[intIndex].FindControl("chkRsSelect");
                    ImageButton ibtnRsCancel = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsCancel");
                    ImageButton ibtnRsEdit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsEdit");
                    ImageButton ibtnRsServer = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsServer");
                    ImageButton ibtnRsOAudit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsOAudit");

                    CuTextBox ctbRs0 = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs0");
                    CuTextSelectBox ctsbRs0 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRs0");
                    CuTextSelectBox ctsbRs1 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRs1");
                    CuTextSelectBox ctsbRs2 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRs2");

                    CuTextSelectBox ctsbRsPayment = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRsPayment");//付款方式
                    CuTextSelectBox ctsbRsShipment = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRsShipment");//运输方式
                    CuTextSelectBox ctsbRsDept = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRsDept");//部门
                    CuTextSelectBox ctsbRsWk_id = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRsWk_id");//采购员
                    CuTextSelectBox ctsbRsDepot = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRsDepot");//仓库
                    CuTextBox ctbRsReciver = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRsReciver");//收货人
                    CuTextBox ctbRsPhone = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRsPhone");//联系电话
                    CuTextBox ctbRsAddr = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRsAddr");//收货地址
                    CuTextBox ctbRsRemark = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRsRemark");//备注

                    Label labId = (Label)this.dgrdResult.Items[intIndex].FindControl("labId");
                    if (intCommandName == 1)//取消编辑
                    {
                        this.GetStockInInfoByMain(strUserID);//重新绑定结果
                    }
                    else if (intCommandName == 2)//编辑
                    {
                        labId.Text = labId.ToolTip;
                        ctsbRs0.Text = ctsbRs0.ToolTip;
                        ctsbRs1.Text = ctsbRs1.ToolTip;
                        ctsbRs2.Text = ctsbRs2.ToolTip;
                        ctsbRsDepot.Text = ctsbRsDepot.ToolTip;
                        ctsbRsPayment.Text = ctsbRsPayment.ToolTip;
                        ctsbRsShipment.Text = ctsbRsShipment.ToolTip;
                        ctsbRsDept.Text = ctsbRsDept.ToolTip;
                        ctsbRsWk_id.Text = ctsbRsWk_id.ToolTip;                        
                        ibtnRsCancel.Visible = ibtnRsServer.Visible = true;
                        chkRsSelect.Visible = ibtnRsEdit.Visible = ibtnRsOAudit.Visible = false;
                        ctsbRs0.ReadOnly = ctsbRs1.ReadOnly = ctsbRs2.ReadOnly = ctsbRsPayment.ReadOnly = false;
                        ctsbRsShipment.ReadOnly = ctsbRsDept.ReadOnly = ctsbRsWk_id.ReadOnly = ctbRsRemark.ReadOnly = false;
                        ctbRsReciver.ReadOnly = ctbRsPhone.ReadOnly = ctbRsAddr.ReadOnly = ctsbRsDepot.ReadOnly = false;
                        this.lblJs.Text = "<script>document.getElementById('" + ctsbRs0.ClientID + "_txtBox').select();document.getElementById('" + ctsbRs0.ClientID + "_txtBox').focus();</script>";
                    }
                    else//保存编辑
                    {
                        Boolean bolIsSucceed = cls_StockManage.AddOrUpdateStockOrder(strType, labId.Text.Trim(), DateTime.Parse(ctsbRs0.Text.Trim()),
                                                                DateTime.Parse(ctsbRs1.Text.Trim()),
                                                ctsbRs2.Text.Trim(), ctsbRsPayment.Text.Trim(), ctsbRsShipment.Text.Trim(), ctsbRsDepot.Text.Trim(),
                                                ctsbRsDept.Text.Trim(), ctsbRsWk_id.Text.Trim(), ctbRsReciver.Text.Trim(), ctbRsPhone.Text.Trim(), 
                                                ctbRsAddr.Text.Trim(), strUserID, ctbRsRemark.Text.Trim(), 1);

                        if (bolIsSucceed)
                        {
                            this.GetStockInInfoByMain(strUserID);//重新绑定结果
                        }
                        else
                        {
                            this.labMessage.Text = "<script language='javascript'>alert('修改记录失败，请重试！');</script>！";                            
                        }
                    }
                }
                //////////////////////////////////////////从表相关操作/////////////////////////////////////
                else
                {
                    DataGrid dgrd = new DataGrid();
                    string strDm_No = "";
                    int intItems = this.dgrdResult.Items.Count;

                    for (int i = intItems - 1; i >= 0; i--)//找当前可见的子DataGrid
                    {
                        Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                        if (panItem.Visible)
                        {
                            dgrd = (DataGrid)this.dgrdResult.Items[i].FindControl("dgrdChildResult");
                            strDm_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRs0")).Text.Trim();
                            break;
                        }
                    }

                    CheckBox chkChildRsSelect = (CheckBox)dgrd.Items[intIndex].FindControl("chkChildRsSelect");//在子DataGrid中找要操作的控件
                    ImageButton ibtnChildRsCancel = (ImageButton)dgrd.Items[intIndex].FindControl("ibtnChildRsCancel");
                    ImageButton ibtnChildRsEdit = (ImageButton)dgrd.Items[intIndex].FindControl("ibtnChildRsEdit");
                    ImageButton ibtnChildRsServer = (ImageButton)dgrd.Items[intIndex].FindControl("ibtnChildRsServer");

                    CuTextSelectBox ctsbChildRs0 = (CuTextSelectBox)dgrd.Items[intIndex].FindControl("ctsbChildRs0");//商品编号
                    CuTextBox ctbChildRs4 = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRs4");//数量
                    CuTextBox ctbChildRs5 = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRs5");//单价
                    CuTextBox ctbChildRs8 = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRs8");//备注                    
                    CuTextBox ctbChildRsBatchNo = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRsBatchNo");//批号                    

                    if (intCommandName == 5)//取消编辑
                    {
                        DataSet dst = cls_StockManage.GetStockOrderChilds(strDm_No, strType);

                        if (dst != null && dst.Tables[0].Rows.Count > 0)
                        {
                            this.BindStockInChildByDataView(dgrd, dst, 0);
                        }
                    }
                    else if (intCommandName == 6)//编辑
                    {                        
                        chkChildRsSelect.Visible = ibtnChildRsEdit.Visible = false;
                        ibtnChildRsCancel.Visible = ibtnChildRsServer.Visible = true;
                        ctbChildRsBatchNo.ReadOnly = false;
                        ctbChildRs4.ReadOnly = ctbChildRs5.ReadOnly = ctbChildRs8.ReadOnly = false;
                        this.lblJs.Text = "<script>document.getElementById('" + ctbChildRs4.ClientID + "_txtBox').select();document.getElementById('" + ctbChildRs4.ClientID + "_txtBox').focus();</script>";
                    }
                    else//保存编辑
                    {
                        int iId = int.Parse(dgrd.Items[intIndex].Cells[1].Text.Trim());

                        Boolean bolIsSucceed = cls_StockManage.AddOrUpdateStockOrderChild(iId, strType, strDm_No, ctsbChildRs0.Text.Trim(),
                            decimal.Parse(ctbChildRs4.Text),decimal.Parse(ctbChildRs5.Text),0, ctbChildRsBatchNo.Text.Trim(),
                            ctbChildRs8.Text, 1);

                        if (bolIsSucceed)//重新绑定从表结果
                        {
                            DataSet dst = cls_StockManage.GetStockOrderChilds(strDm_No, strType);

                            if (dst != null && dst.Tables[0].Rows.Count > 0)
                            {
                                this.BindStockInChildByDataView(dgrd, dst, 0);
                            }
                        }
                        else
                        {
                            this.labMessage.Text = "<script language='javascript'>alert('修改记录失败，请重试！');</script>！";                            
                        }
                    }
                }
            }
            else
            {
                this.Page.Session.Remove("SetOrGetUserID");
                this.Page.RegisterClientScriptBlock("Js", "<script>parent.window.location.href='../index.aspx'</script>");
            }
        }
        catch
        {
            this.labMessage.Text = "<script language='javascript'>alert('修改记录失败，请重试！');</script>！";
        }
    }

    public void lbtnDelete_Click(object sender, EventArgs e)
    {
        int intCommandName = Int32.Parse(((LinkButton)sender).CommandName);

        this.DeleteStockCheckInfo(intCommandName);
    }

    public void OpenChildResult(object sender, ImageClickEventArgs e)
    {
        string strUserID = this.getCurUserId();
        ImageButton ibtnOpen = (ImageButton)sender;
        int intCommandName = Int32.Parse(ibtnOpen.CommandName);
        int intIndex = Int32.Parse(ibtnOpen.CommandArgument);
        int intCount = this.dgrdResult.Items.Count;
        bool is_audit = bool.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text);
        bool Is_visible = bool.Parse(this.dgrdResult.Items[intIndex].Cells[3].Text.Trim());//是否作废        

        CheckBox chkRsSelect = (CheckBox)this.dgrdResult.Items[intIndex].FindControl("chkRsSelect");
        ImageButton ibtnOpenChild = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnOpenChild");
        ImageButton ibtnRsCancel = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsCancel");
        ImageButton ibtnRsEdit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsEdit");
        ImageButton ibtnRsServer = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsServer");
        ImageButton ibtnRsOAudit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsOAudit");
        Panel panShowChildResult = (Panel)this.dgrdResult.Items[intIndex].FindControl("panShowChildResult");

        CuTextBox ctbRs0 = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRs0");//显示从表信息       
        ClsStockManageDAL cls_StockManage = new ClsStockManageDAL();
        DataSet dst = cls_StockManage.GetStockOrderChilds(ctbRs0.Text.Trim(), strType);

        if (dst == null || dst.Tables[0].Rows.Count <= 0)
        {
            ibtnOpen.Attributes.Add("OnClick", "return confirm('单据录入未完成，您要删除此单据吗？');");
        }

        if (intCommandName == 0)//展开
        {            
            for (int i = 0; i < intCount; i++)//找到展开的项
            {
                Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                if (i == intIndex)
                {
                    panItem.Visible = true;
                }
                else
                {
                    panItem.Visible = false;
                }                
            }

            this.ibtnAdd.CommandName = "5";//主表展开后则不可操作
            this.ibtnRef.CommandName = "1";
            this.ibtnAdd.CommandArgument = intIndex.ToString();
            panShowChildResult.Visible = true;
            ibtnOpenChild.CommandName = "1";
            ibtnOpenChild.ImageUrl = "../App_Images/ico/10.gif";
            ibtnRsEdit.ImageUrl = "../App_Images/ico/14.gif";
            ibtnRsServer.ImageUrl = "../App_Images/ico/16.gif";
            ibtnRsOAudit.ImageUrl = "../App_Images/ico/17.gif";
            ibtnRsCancel.ImageUrl = "../App_Images/ico/18.gif";
            this.ibtnAudit.ImageUrl = "../App_Images/b_21_3.gif";
            this.ibtnAudit.Enabled = chkRsSelect.Enabled = ibtnRsCancel.Enabled = ibtnRsEdit.Enabled = ibtnRsServer.Enabled = ibtnRsOAudit.Enabled = false;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.lblGoTxt1.Enabled = this.txtGo.Enabled = this.lblGoTxt2.Enabled = false;

            if (is_audit)//如果主表确认后则从表不可操作
            {
                this.ibtnAdd.Enabled = this.ibtnDelete0.Enabled = false;
                this.ibtnAdd.ImageUrl = "../App_Images/b_18_3.gif";
                this.ibtnDelete0.ImageUrl = "../App_Images/b_19_5.gif";
                this.imgDelete.ImageUrl = "../App_Images/b_19_6.gif";
                this.imgDelete.Attributes.Add("OnMouseOver","");
                this.imgDelete.Attributes.Add("OnMouseOut", "");
                this.imgDelete.Attributes.Add("OnClick", "");
            }

            //绑定从表信息
            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                DataGrid dgrd = (DataGrid)this.dgrdResult.Items[intIndex].FindControl("dgrdChildResult");
                this.BindStockInChildByDataView(dgrd, dst, 0);
            }
        }
        else//收缩
        {
            this.ibtnAdd.CommandName = "0";
            this.ibtnRef.CommandName = "0";
            panShowChildResult.Visible = false;
            ibtnOpenChild.CommandName = "0";
            ibtnOpenChild.ImageUrl = "../App_Images/ico/9.gif";
            ibtnRsEdit.ImageUrl = "../App_Images/ico/12.gif";
            ibtnRsServer.ImageUrl = "../App_Images/ico/11.gif";
            ibtnRsOAudit.ImageUrl = "../App_Images/ico/8.gif";
            ibtnRsCancel.ImageUrl = "../App_Images/ico/4.gif";
            this.ibtnAudit.ImageUrl = "../App_Images/b_21_1.gif";
            this.ibtnAdd.ImageUrl = "../App_Images/b_18_1.gif";
            this.ibtnDelete0.ImageUrl = "../App_Images/b_19_1.gif";
            this.imgDelete.ImageUrl = "../App_Images/b_19_4.gif";
            this.imgDelete.Attributes.Add("OnMouseOver", "ChangImage(this, '../App_Images/b_19_3.gif');ChangImage(document.all.ibtnDelete0, '../App_Images/b_19_2.gif')");
            this.imgDelete.Attributes.Add("OnMouseOut", "ChangImage(this, '../App_Images/b_19_4.gif');ChangImage(document.all.ibtnDelete0, '../App_Images/b_19_1.gif')");
            this.imgDelete.Attributes.Add("OnClick", "ChildMenuVisible()");
            this.ibtnAdd.Enabled = this.ibtnDelete0.Enabled = this.ibtnAudit.Enabled = chkRsSelect.Enabled = ibtnRsCancel.Enabled = ibtnRsEdit.Enabled = ibtnRsServer.Enabled = ibtnRsOAudit.Enabled = true;

            if (dst == null || dst.Tables[0].Rows.Count <= 0)
            {
                Boolean bolIsSucceed = cls_StockManage.DeleteStockOrderState(ctbRs0.Text.Trim(), 3, strType);
                this.GetStockInInfoByMain(strUserID);//重新绑定数据
            }
            else
            {
                //重新绑定
                ClsPropertyDAL cls_Property = new ClsPropertyDAL();
                dst = cls_Property.SetOrGetCorporationDataSet;
                this.BindStockInByDataView(dst, this.dgrdResult.CurrentPageIndex);
                //this.GetStockInInfoByMain(strUserID);//重新绑定数据
            }
        }
    }

    private void GetStockInInfoByMain(string strCurUserID)
    {
        ClsStockManageDAL cls_StockManage = new ClsStockManageDAL();
                
        DataSet dst = cls_StockManage.GetStockOrderMains(4, strCurUserID);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            cls_Property.SetOrGetCorporationDataSet = dst;

            this.BindStockInByDataView(dst, 0);
        }
        else
        {
            this.dgrdResult.CurrentPageIndex = 0;
            this.dgrdResult.DataSource = null;
            this.dgrdResult.DataBind();
            this.lblRsCounts.Text = "共0条记录";
            this.lblPageCounts.Text = "共0页";
            this.lblCurrPage.Text = "当前第0页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = false;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lblGoTxt1.Enabled = this.lblGoTxt2.Enabled = false;
        } 
    }

    private void BindStockInByDataView(DataSet dst, int CurrPageIndex)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdResult.CurrentPageIndex = CurrPageIndex;
            this.dgrdResult.DataSource = dst;
            this.dgrdResult.DataBind();

            this.lblRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
            this.lblPageCounts.Text = "共" + this.dgrdResult.PageCount + "页";
            this.lblCurrPage.Text = "当前第" + (this.dgrdResult.CurrentPageIndex + 1) + "页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = true;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lblGoTxt1.Enabled = this.lblGoTxt2.Enabled = true;

            if (this.dgrdResult.PageCount == 1)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
                this.lblGoTxt1.Enabled = this.txtGo.Enabled = this.lblGoTxt2.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == this.dgrdResult.PageCount - 1)
            {
                this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == 0)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = false;
            }
        }
        else
        {
            this.dgrdResult.DataSource = null;
            this.dgrdResult.DataBind();
        }
    }

    private void BindStockInChildByDataView(DataGrid dgrd, DataSet dst, int CurrPageIndex)
    {
        if (dgrd != null && dst != null)
        {
            dgrd.CurrentPageIndex = CurrPageIndex;
            dgrd.DataSource = dst;
            dgrd.DataBind();

            int intIndex = Int32.Parse(dgrd.ToolTip);
            int intItems = dgrd.Items.Count;

            Label lblChildRsCounts = (Label)this.dgrdResult.Items[intIndex].FindControl("lblChildRsCounts");
            Label lblChildPageCounts = (Label)this.dgrdResult.Items[intIndex].FindControl("lblChildPageCounts");
            Label lblChildCurrPage = (Label)this.dgrdResult.Items[intIndex].FindControl("lblChildCurrPage");
            LinkButton lbtnChildFirst = (LinkButton)this.dgrdResult.Items[intIndex].FindControl("lbtnChildFirst");
            LinkButton lbtnChildPrev = (LinkButton)this.dgrdResult.Items[intIndex].FindControl("lbtnChildPrev");
            LinkButton lbtnChildNext = (LinkButton)this.dgrdResult.Items[intIndex].FindControl("lbtnChildNext");
            LinkButton lbtnChildEnd = (LinkButton)this.dgrdResult.Items[intIndex].FindControl("lbtnChildEnd");

            lblChildRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条明细记录";
            lblChildPageCounts.Text = "共" + dgrd.PageCount + "页";
            lblChildCurrPage.Text = "当前第" + (dgrd.CurrentPageIndex + 1) + "页";
            lbtnChildFirst.Enabled = lbtnChildPrev.Enabled = lbtnChildNext.Enabled = lbtnChildEnd.Enabled = true;
            lblChildRsCounts.Enabled = lblChildPageCounts.Enabled = lblChildCurrPage.Enabled = true;

            if (dgrd.PageCount == 1)
            {
                lbtnChildFirst.Enabled = lbtnChildPrev.Enabled = lbtnChildNext.Enabled = lbtnChildEnd.Enabled = false;
            }
            else if (dgrd.CurrentPageIndex == dgrd.PageCount - 1)
            {
                lbtnChildNext.Enabled = lbtnChildEnd.Enabled = false;
            }
            else if (dgrd.CurrentPageIndex == 0)
            {
                lbtnChildFirst.Enabled = lbtnChildPrev.Enabled = false;
            }

            //判断从表中的记录是否可操作
            //for (int i = 0; i < intItems; i++)
            //{
            //    CheckBox chkChildRsSelect = (CheckBox)dgrd.Items[i].FindControl("chkChildRsSelect");
            //    ImageButton ibtnChildRsEdit = (ImageButton)dgrd.Items[i].FindControl("ibtnChildRsEdit");

            //    if (!this.ibtnAdd.Enabled)
            //    {
            //        chkChildRsSelect.Enabled = ibtnChildRsEdit.Enabled = false;
            //        ibtnChildRsEdit.ImageUrl = "../App_Images/ico/14.gif";
            //    }
            //    else
            //    {
            //        chkChildRsSelect.Enabled = ibtnChildRsEdit.Enabled = true;
            //        ibtnChildRsEdit.ImageUrl = "../App_Images/ico/12.gif";
            //    }
            //}

            //保存从表中的结果集和DataGrid控件用于从表中的翻页
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            cls_Property.SetOrGetChildResultDataSet = dst;
            cls_Property.SetOrGetChildDataGridControl = dgrd;
        }
    }

    private void DeleteStockCheckInfo(int CommandName)
    {
        try
        {
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            string strUserID = this.getCurUserId();

            if (strUserID != "")
            {
                ClsStockManageDAL cls_StockManage = new ClsStockManageDAL();
                int intCount = this.dgrdResult.Items.Count;
                int intcommand = Int32.Parse(this.ibtnRef.CommandName);

                if (intcommand == 0)//删除主表
                {
                    for (int i = 0; i < intCount; i++)
                    {
                        bool IsChecked = ((CheckBox)this.dgrdResult.Items[i].FindControl("chkRsSelect")).Checked;

                        if (IsChecked)
                        {
                            Boolean bIs_audit = Boolean.Parse(this.dgrdResult.Items[i].Cells[1].Text.Trim());//确认状态
                            Boolean bIs_end = Boolean.Parse(this.dgrdResult.Items[i].Cells[2].Text.Trim());//终止状态

                            Boolean bolIsSucceed = false;
                            string strDm_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRs0")).Text.Trim();

                            if (bIs_audit)
                            {
                                this.labMessage.Text = "<script language='javascript'>alert('此单已确认，不能删除！');</script>！";
                            }
                            else if (bIs_end)
                            {
                                this.labMessage.Text = "<script language='javascript'>alert('此单已终止，不能删除！');</script>！";
                            }
                            else if (CommandName == 0)//不彻底删除
                            {
                                bolIsSucceed = cls_StockManage.DeleteStockOrderState(strDm_No, 2, strType);
                                if (!bolIsSucceed)
                                {
                                    this.labMessage.Text = "<script language='javascript'>alert('删除记录失败，请重试！');</script>！";
                                }
                            }
                            else//彻底删除
                            {
                                bolIsSucceed = cls_StockManage.DeleteStockOrderState(strDm_No, 3, strType);
                                if (!bolIsSucceed)
                                {
                                    this.labMessage.Text = "<script language='javascript'>alert('删除记录失败，请重试！');</script>！";
                                }
                            }
                        }
                    }

                    //重新绑定主表结果
                    this.GetStockInInfoByMain(strUserID);
                }
                else//删除从表
                {
                    DataGrid dgrd = new DataGrid();
                    string strDm_No = "";
                    int intItems = this.dgrdResult.Items.Count;

                    for (int i = intItems - 1; i >= 0; i--)//找当前可见的子DataGrid
                    {
                        Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                        if (panItem.Visible)
                        {
                            dgrd = (DataGrid)this.dgrdResult.Items[i].FindControl("dgrdChildResult");//labId
                            strDm_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRs0")).Text.Trim();
                            break;
                        }
                    }

                    int intChildItems = dgrd.Items.Count;//循环删除所选的项

                    for (int j = intChildItems - 1; j >= 0; j--)
                    {
                        bool IsChecked = ((CheckBox)dgrd.Items[j].FindControl("chkChildRsSelect")).Checked;

                        if (IsChecked)
                        {
                            string strId = dgrd.Items[j].Cells[1].Text.Trim();
                            Boolean bolIsSucceed = false;

                            if (CommandName == 0)//不彻底删除
                            {
                                bolIsSucceed = cls_StockManage.DeleteStockOrderChildState(strDm_No, strId, 2, strType);
                            }
                            else//彻底删除
                            {
                                bolIsSucceed = cls_StockManage.DeleteStockOrderChildState(strDm_No, strId, 3, strType);
                            }

                            if (!bolIsSucceed)
                            {
                                this.labMessage.Text = "<script language='javascript'>alert('删除记录失败，请重试！');</script>！";
                            }
                        }
                    }

                    DataSet dst = cls_StockManage.GetStockOrderChilds(strDm_No, strType);//重新绑定从表结果

                    if (dst != null && dst.Tables[0].Rows.Count > 0)
                    {
                        this.BindStockInChildByDataView(dgrd, dst, 0);
                    }
                    else
                    {
                        dgrd.DataSource = null;
                        dgrd.DataBind();
                    }
                }
            }
            else
            {
                this.Page.Session.Remove("SetOrGetUserID");
                this.Page.RegisterClientScriptBlock("Js", "<script>parent.window.location.href='../index.aspx'</script>");
            }
        }
        catch
        {
            this.labMessage.Text = "<script language='javascript'>alert('删除记录失败，请重试！');</script>！";
        }
    }   

    protected void ibtnDelete0_Click(object sender, ImageClickEventArgs e)
    {
        int intCommandName = Int32.Parse(((ImageButton)sender).CommandName);

        this.DeleteStockCheckInfo(intCommandName);
    }

    protected void txtGo_TextChanged(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strIndex = this.txtGo.Text.Trim();
            this.txtGo.Text = String.Empty;

            if (strIndex != "")
            {
                try
                {
                    int intIndex = Int32.Parse(strIndex) - 1;

                    if (intIndex >= 0 && intIndex < this.dgrdResult.PageCount)
                    {
                        this.BindStockInByDataView(dst, intIndex);
                    }
                    else
                    {
                        this.labMessage.Text = "<script language='javascript'>alert('请输入正确的页码数！');</script>！";                        
                    }
                }
                catch
                {
                    this.labMessage.Text = "<script language='javascript'>alert('请输入数字类型的页码！');</script>！";                    
                }
            }
            else
            {
                this.labMessage.Text = "<script language='javascript'>alert('请输入要跳转的页码！');</script>！";                
            }
        }
    }

    protected void ibtnRef_Click(object sender, ImageClickEventArgs e)
    {
        string strUserID = this.getCurUserId();
        int intCommand = Int32.Parse(((ImageButton)sender).CommandName);

        if (intCommand == 0)//刷新主表
        {
            this.GetStockInInfoByMain(strUserID);
        }
        else//刷新从表
        {
            DataGrid dgrd = new DataGrid();
            string strDm_No = "";
            int intItems = this.dgrdResult.Items.Count;

            for (int i = intItems - 1; i >= 0; i--)//找当前可见的子DataGrid
            {
                Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                if (panItem.Visible)
                {
                    dgrd = (DataGrid)this.dgrdResult.Items[i].FindControl("dgrdChildResult");
                    strDm_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRs0")).Text.Trim();
                    break;
                }
            }

            ClsStockManageDAL cls_StockManage = new ClsStockManageDAL();
            DataSet dst = cls_StockManage.GetStockOrderChilds(strDm_No, strType);

            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                this.BindStockInChildByDataView(dgrd, dst, 0);
            }
        }
    }

    protected void dgrdResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            bool Is_audit = bool.Parse(e.Item.Cells[1].Text.Trim());
            bool Is_end = bool.Parse(e.Item.Cells[2].Text.Trim());
            bool Is_visible = bool.Parse(e.Item.Cells[3].Text.Trim());//是否作废

            CheckBox chkRsSelect = (CheckBox)e.Item.FindControl("chkRsSelect");
            CuTextBox ctbRsRemark = (CuTextBox)e.Item.FindControl("ctbRsRemark");
            ImageButton ibtnRsEdit = (ImageButton)e.Item.FindControl("ibtnRsEdit");
            ImageButton ibtnRsCancel = (ImageButton)e.Item.FindControl("ibtnRsCancel");
            ImageButton ibtnRsServer = (ImageButton)e.Item.FindControl("ibtnRsServer");
            ImageButton ibtnRsOAudit = (ImageButton)e.Item.FindControl("ibtnRsOAudit");

            //修改按回车提交
            if (ctbRsRemark.ReadOnly)
            {
                ctbRsRemark.Attributes.Add("onkeydown", "if(event.keyCode == 13){document.getElementById('" + ibtnRsServer.ClientID + "').click();}");
            }

            if (Is_visible)//是否作废
            {
                chkRsSelect.Enabled = ibtnRsEdit.Enabled = false;
                ibtnRsEdit.ImageUrl = "../App_Images/ico/14.gif";
            }

            string strButtonEdit = this.hidButtonEdit.Value;
            string strButtonOAudit = this.hidButtonOAudit.Value;

            if (!Is_audit)
            {
                chkRsSelect.Visible = true;
                ibtnRsEdit.Visible = true;

                //可编辑
                if (strButtonEdit.Trim() == "1")
                {
                    //不可选择
                    if (!this.panAudit.Visible && !this.panDelete.Visible)//!this.panEnd.Visible &&
                    {
                        chkRsSelect.Enabled = false;
                    }
                }
                else//不可编辑
                {
                    ibtnRsEdit.Enabled = false;
                    ibtnRsEdit.ImageUrl = "../App_Images/ico/14.gif";
                    ibtnRsEdit.ToolTip = "无编辑权限";

                    //不可选择
                    if (!this.panAudit.Visible  && !this.panDelete.Visible)//&& !this.panEnd.Visible
                    {
                        chkRsSelect.Enabled = false;
                    }
                }
            }
            else
            {
                ibtnRsOAudit.Visible = chkRsSelect.Visible = true;

                if (strButtonOAudit.Trim() != "5")
                {
                    ibtnRsOAudit.Enabled = false;
                    ibtnRsOAudit.ImageUrl = "../App_Images/ico/17.gif";
                }

                //判断是否已终止
                //if (Is_end)
                //{
                //    ibtnRsEdit.Visible = false;
                //    ibtnRsCancel.Visible = false;
                //    ibtnRsServer.Visible = false;
                //    chkRsSelect.Visible = true;
                //    ibtnRsOAudit.Visible = true;
                //    ibtnRsOAudit.Enabled = false;
                //    ibtnRsOAudit.ImageUrl = "../App_Images/ico/17.gif";
                //    ibtnRsOAudit.ToolTip = "此单已终止";

                //    if (!this.panAudit.Visible && !this.panDelete.Visible)//&& !this.panEnd.Visible
                //    {
                //        chkRsSelect.Enabled = false;
                //    }
                //}
            }
        }
    }

    protected void dgrdResult_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType.ToString() == "Item" || e.Item.ItemType.ToString() == "AlternatingItem")
        {
            DataGrid dgrdChildResult = (DataGrid)e.Item.FindControl("dgrdChildResult");
            //子DataGrid注册事件
            dgrdChildResult.ItemDataBound += new DataGridItemEventHandler(dgrdChildResult_ItemDataBound);
        }
    }

    protected void dgrdChildResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {        
        if (e.Item.ItemIndex != -1)
        {
            int iIndex = int.Parse(((DataGrid)sender).ToolTip);
            bool Is_audit = bool.Parse(this.dgrdResult.Items[iIndex].Cells[1].Text.Trim());//是否确认
            bool Is_visible = bool.Parse(this.dgrdResult.Items[iIndex].Cells[3].Text.Trim());//是否作废
            CheckBox chkChildRsSelect = (CheckBox)e.Item.FindControl("chkChildRsSelect");
            ImageButton ibtnChildRsEdit = (ImageButton)e.Item.FindControl("ibtnChildRsEdit");
            if (Is_audit || Is_visible)//是否作废
            {
                chkChildRsSelect.Enabled = ibtnChildRsEdit.Enabled = false;
                ibtnChildRsEdit.ImageUrl = "../App_Images/ico/14.gif";
            }

            CuTextBox ctbprice = (CuTextBox)e.Item.FindControl("ctbChildRs3");//单价
            CuTextBox ctbsellprice = (CuTextBox)e.Item.FindControl("ctbChildRs5");//售价
            CuTextBox ctbmoney = (CuTextBox)e.Item.FindControl("ctbChildRs7");//金额
            CuTextBox ctbqty = (CuTextBox)e.Item.FindControl("ctbChildRs4");//数量            

            //小数位数控制
            string strSystemParam = this.hidSystemParam.Value.Trim();

            if (strSystemParam != "")
            {
                string[] strParam = strSystemParam.Split(new char[] { '|' });

                //价格小数设置
                bool is_setPrice = bool.Parse(strParam[0].ToString());
                int intCountPrice = Int32.Parse(strParam[1].ToString());
                int intTypePrice = Int32.Parse(strParam[2].ToString());

                if (is_setPrice)
                {
                    if (intTypePrice == 0)//四舍五入
                    {
                        decimal dprice = Math.Round(decimal.Parse(ctbprice.Text), intCountPrice);
                        decimal dsellprice = Math.Round(decimal.Parse(ctbsellprice.Text), intCountPrice);
                        decimal dmoney = Math.Round(decimal.Parse(ctbmoney.Text), intCountPrice);
                        ctbprice.Text = dprice.ToString();
                        ctbsellprice.Text = dsellprice.ToString();
                        ctbmoney.Text = dmoney.ToString();
                    }
                }

                //数量小数设置
                bool is_setCount = bool.Parse(strParam[3].ToString());
                int intCountCount = Int32.Parse(strParam[4].ToString());
                int intTypeCount = Int32.Parse(strParam[5].ToString());

                if (is_setCount)
                {
                    if (intTypeCount == 0)//四舍五入
                    {
                        decimal dqty = Math.Round(decimal.Parse(ctbqty.Text), intCountCount);
                        ctbqty.Text = dqty.ToString();
                    }
                }
            }
        }               
    }

    //终止
    //protected void ibtnEnd_Click(object sender, ImageClickEventArgs e)
    //{
    //    ClsPropertyDAL cls_Property = new ClsPropertyDAL();

    //    string strUserID = cls_Property.SetOrGetUserID;

    //    if (strUserID != "")
    //    {
    //        int intCount = this.dgrdResult.Items.Count;

    //        for (int i = 0; i < intCount; i++)
    //        {
    //            bool IsChecked = ((CheckBox)this.dgrdResult.Items[i].FindControl("chkRsSelect")).Checked;

    //            if (IsChecked)
    //            {
    //                string strDm_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRs0")).Text.Trim();

    //                ClsStockManageDAL cls_StockManage = new ClsStockManageDAL();
    //                Boolean bolIsSucceed = cls_StockManage.EndBOD_no(strType, "tboDueM", "dm_no", strDm_No, "dm_type");                    

    //                if (!bolIsSucceed)
    //                {
    //                    this.lblJs.Text = "<script>alert('终止失败，请重试！')</script>";
    //                }
    //            }
    //        }

    //        //重新绑定主表结果
    //        this.GetStockInInfoByMain(strUserID);
    //    }
    //    else
    //    {
    //        this.Page.Session.Remove("SetOrGetUserID");
    //        this.Page.RegisterClientScriptBlock("Js", "<script>parent.window.location.href='../index.aspx'</script>");
    //    }
    //}

    ////根据订单状态进行相关查询

    public void lbtnQuery_Click(object sender, EventArgs e)
    {
        int intCommandName = Int32.Parse(((LinkButton)sender).CommandName);

        this.QueryOrderByStatu(intCommandName);
    }

    private void QueryOrderByStatu(int CommandName)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ClsStockManageDAL cls_StockManage = new ClsStockManageDAL();
            DataSet dst = cls_StockManage.GetOrderInformationByStatu(strUserID,"tboDueM", int.Parse(strType), CommandName);

            cls_Property.SetOrGetCorporationDataSet = dst;
            this.BindStockInByDataView(dst, 0);                     
        }
    }

    //得到当前用户ID(公用)
    private string getCurUserId()
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        string strUserID = cls_Property.SetOrGetUserID;
        if (strUserID.Length > 0)
        {
            return strUserID;
        }
        else
        {
            return "";
        }
    }
}
